package com.ccrfid.rmc6.util;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;

public enum GeomUtil {
    INSTANCE;
    private GeometryFactory geometryFactory;

    GeomUtil() {
        this.geometryFactory = new GeometryFactory();
    }

    public Point createPoint(Double x, Double y) {
        if (x == null || y == null) {
            return null;
        }
        return geometryFactory.createPoint(new Coordinate(x, y));
    }

    public Point getInvisiblePoint() {
        return geometryFactory.createPoint(new Coordinate(-10000.0, -10000.0));
    }

    public Polygon createPolygon(double x1, double y1, double x2, double y2) {
        Coordinate[] coordinates = {
                new Coordinate(x1, y1), new Coordinate(x2, y1),
                new Coordinate(x2, y2), new Coordinate(x1, y2),
                new Coordinate(x1, y1)
        };
        return geometryFactory.createPolygon(coordinates);
    }

    public Polygon createSmallPolygon(double x, double y) {
        int base = 3;
        double x1 = x - base > 0 ? x - base : 0;
        double x2 = x + base;
        double y1 = y - base > 0 ? y - base : 0;
        double y2 = y + base;
        Coordinate[] coordinates = {
                new Coordinate(x1, y1), new Coordinate(x2, y1),
                new Coordinate(x2, y2), new Coordinate(x1, y2),
                new Coordinate(x1, y1)
        };
        return geometryFactory.createPolygon(coordinates);
    }
}
